package ${package.Controller};


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * ${table.comment} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>
    @Autowired
    private ${table.serviceName} ${cfg.serviceInstance};

    private ObjectMapper mapper = new ObjectMapper();

    @RequestMapping("")
    public ModelAndView init() {
    return new ModelAndView("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>");
    }

    /**
    * 查询列表
    * @param currentPage
    * @param pageSize
    * @return
    */
    @RequestMapping("list/{currentPage}/{pageSize}")
    @ResponseBody
    public Page<${entity}> findList(@PathVariable String currentPage, @PathVariable String pageSize,@RequestBody ${entity} ${table.entityPath}) {
        EntityWrapper<${entity}> ew = new EntityWrapper<>();
        Page<${entity}> page = new Page<>(Integer.valueOf(currentPage),Integer.valueOf(pageSize));
        ew.like("name",${table.entityPath}.getName())
            .orderDesc(Arrays.asList("update_date"));
        ${cfg.serviceInstance}.selectPage(page,ew);
        return page;
    }

    /**
    * 删除单条记录
    * @param ${table.entityPath}
    * @return
    */
    @RequestMapping("delete")
    @ResponseBody
    public boolean delete(@RequestBody ${entity} ${table.entityPath}) {
        return ${cfg.serviceInstance}.deleteById(${table.entityPath}.getId());
    }

    /**
    * 保存记录
    * @param ${table.entityPath}
    * @return
    */
    @RequestMapping("save")
    @ResponseBody
    public boolean save(@RequestBody ${entity} ${table.entityPath}) {
        return ${cfg.serviceInstance}.insertOrUpdate(${table.entityPath});
    }

    /**
    * 批量删除
    * @param map
    * @return
    */
    @RequestMapping("batchDelete")
    @ResponseBody
    public boolean batchDelete(@RequestBody Map<String,Object> map) {
        String ids = String.valueOf(map.get("ids"));
        List<Long> idsList = null;
        try {
        idsList = mapper.readValue(ids, new TypeReference<List<Long>>() {});
        } catch (IOException e) {
        e.printStackTrace();
        }
        return ${cfg.serviceInstance}.deleteBatchIds(idsList);
    }
}

</#if>
